import { defineStore } from "pinia";
import { WebPlatformSysConfig } from "@/_types/system/system-config";

export const useSysconfigrStore = defineStore({
  id: "jxtech-sysconfig",
  state: (): WebPlatformSysConfig => ({
    /**新增用户*/
    addPerson: false,
    /**应用id*/
    appId: "",
    /**应用名称*/
    appName: "",
    /**请求头名称*/
    authHeaderName: "",
    /**应用id*/
    clientid: "",
    cmsExist: false,
    /**公司名称*/
    corp: "",
    /**当前得应用id*/
    currentComp: "",
    /**数据库模式*/
    databaseSchema: "",
    /**是否加密密码*/
    encodePassword: false,
    encriptType: "",
    /**网关地址*/
    gatewayUrl: "",
    haveSubOrg: false,
    /**是否开启国际化*/
    i18n: false,
    initMenuButtonVisible: false,
    /**语言*/
    locale: "",
    /**登录背景 */
    loginBgc: "",
    loginBluesysLogo: "",
    loginBox: "",
    loginDarkLogo: "",
    loginLightLogo: "",
    /**刷新token地址*/
    loginRefreshToken: "",
    /**登录类型*/
    loginType: "",
    /**信息是否显示旗帜 */
    messageExist: false,
    /**登录地址*/
    oauth2Url: "",
    preventRepeat: false,
    pwdValidDays: 0,
    pwdValidEnabled: false,
    pwdValidHintDays: 0,
    /**mq 密码*/
    rabbitPassword: "",
    /**队列类型*/
    rabbitQueuetype: "",
    /**mq 用户名*/
    rabbitUsername: "",
    /**是否开启验证码*/
    randomCode: false,
    rememberMe: false,
    rsaPublicKey: "",
    sessiontimeout: 0,
    showTrace: false,
    /**租户管理部门*/
    tenantManagerUnit: "",
    thirdSSOHeaderName: "",
    /**用户账号类型*/
    userAccountType: "",
    userHaveOnlyOneUnit: false,
    userLevel: false,
    /**ws路径*/
    websocketPath: "",
    /**代办是否显示旗帜 */
    workflowExist: false,
    /**aes key*/
    aeskey: "",
    /**aes iv*/
    aesiv: "",
    webPreviewIp: "",
    webSourceIp: "",
    /**minio储存桶名称 */
    webMinioBucket: "",
    /**minio切片大小 */
    webMinioBigFileSize: 5 * 1024 * 1024,
    /**两种模式 server minio*/
    webUploadMode: "server",
    /**是否可以选择布局 */
    webLayoutSwitch: true,
    /**默认布局 */
    webLayoutDefault: "basicLayout",
    /**默认主题 */
    webThemeDefault: "",
    /**下载文件延时 */
    webDownloadFileTimeout: 12 * 1000,
    /**导航类型 */
    webNavigationType: "menuTab",
    appUrl: "",
    deployType: "",
    ssoProvider: "",
    ssoUrl: "",
    uaaParam: "",
    webI18nSwitch: true,
    // 动态配置图片
    webLoginBg: "",
    webLoginLeftPic: "",
    webLoginLogo: "",
    webPageLogo: "",
    favicon: ""
  }),
  getters: {},
  actions: {
    setSysConfig(sysconfig: WebPlatformSysConfig) {
      if (!sysconfig) return;

      //处理自定义配置
      Object.getOwnPropertyNames(sysconfig).forEach(key => {
        if (!this.hasOwnProperty(key)) {
          this[key] = sysconfig[key];
        }
      });

      this.addPerson = sysconfig.addPerson;
      this.appId = sysconfig.appId;
      this.appName = sysconfig.appName;
      this.authHeaderName = sysconfig.authHeaderName;
      this.clientid = sysconfig.clientid;
      this.cmsExist = sysconfig.cmsExist;
      this.corp = sysconfig.corp;
      this.currentComp = sysconfig["currentComp"];
      this.databaseSchema = sysconfig.databaseSchema;
      this.encodePassword = sysconfig.encodePassword;
      this.encriptType = sysconfig.encriptType;
      this.gatewayUrl = sysconfig["gatewayUrl"];
      this.haveSubOrg = sysconfig.haveSubOrg;
      this.i18n = sysconfig.i18n;
      this.initMenuButtonVisible = sysconfig.initMenuButtonVisible;
      this.locale = sysconfig.locale;
      this.loginBgc = sysconfig.loginBgc;
      this.loginBluesysLogo = sysconfig.loginBluesysLogo;
      this.loginBox = sysconfig.loginBox;
      this.loginDarkLogo = sysconfig.loginDarkLogo;
      this.loginLightLogo = sysconfig.loginLightLogo;
      this.loginRefreshToken = sysconfig.loginRefreshToken;
      this.loginType = sysconfig.loginType;
      this.messageExist = sysconfig.messageExist;
      this.oauth2Url = sysconfig.oauth2Url;
      this.preventRepeat = sysconfig.preventRepeat;
      this.pwdValidDays = sysconfig.pwdValidDays;
      this.pwdValidEnabled = sysconfig.pwdValidEnabled;
      this.pwdValidHintDays = sysconfig.pwdValidHintDays;
      this.rabbitPassword = sysconfig.rabbitPassword;
      this.rabbitQueuetype = sysconfig.rabbitQueuetype;
      this.rabbitUsername = sysconfig.rabbitUsername;
      this.randomCode = sysconfig.randomCode;
      this.rememberMe = sysconfig.rememberMe;
      this.rsaPublicKey = sysconfig.rsaPublicKey;
      this.sessiontimeout = sysconfig.sessiontimeout;
      this.showTrace = sysconfig.showTrace;
      this.tenantManagerUnit = sysconfig.tenantManagerUnit;
      this.thirdSSOHeaderName = sysconfig.thirdSSOHeaderName;
      this.userAccountType = sysconfig.userAccountType;
      this.userHaveOnlyOneUnit = sysconfig.userHaveOnlyOneUnit;
      this.userLevel = sysconfig.userLevel;
      this.websocketPath = sysconfig.websocketPath;
      this.workflowExist = sysconfig.workflowExist;
      this.aeskey = sysconfig.aeskey;
      this.aesiv = sysconfig.aesiv;

      this.appUrl = sysconfig.appUrl;
      this.deployType = sysconfig.deployType;
      this.ssoProvider = sysconfig.ssoProvider;
      this.ssoUrl = sysconfig.ssoUrl;
      this.uaaParam = sysconfig.uaaParam;

      //前端属性的添加
      if (sysconfig.webPreviewIp) this.webPreviewIp = sysconfig.webPreviewIp;
      if (sysconfig.webSourceIp) this.webSourceIp = sysconfig.webSourceIp;
      if (sysconfig.webMinioBucket) this.webMinioBucket = sysconfig.webMinioBucket;
      if (sysconfig.webMinioBigFileSize) this.webMinioBigFileSize = Number(sysconfig.webMinioBigFileSize);
      if (sysconfig.webUploadMode) this.webUploadMode = sysconfig.webUploadMode;
      if (sysconfig.webLayoutSwitch) this.webLayoutSwitch = sysconfig.webLayoutSwitch;
      if (sysconfig.webLayoutDefault) this.webLayoutDefault = sysconfig.webLayoutDefault;
      if (sysconfig.webThemeDefault) this.webThemeDefault = sysconfig.webThemeDefault;
      if (sysconfig.webDownloadFileTimeout) this.webDownloadFileTimeout = Number(sysconfig.webDownloadFileTimeout);
      if (sysconfig.webNavigationType) this.webNavigationType = sysconfig.webNavigationType;
      if (sysconfig.webI18nSwitch) this.webI18nSwitch = sysconfig.webI18nSwitch;

      if (sysconfig.webLoginBg) this.webLoginBg = sysconfig.webLoginBg;
      if (sysconfig.webLoginLeftPic) this.webLoginLeftPic = sysconfig.webLoginLeftPic;
      if (sysconfig.webLoginLogo) this.webLoginLogo = sysconfig.webLoginLogo;
      if (sysconfig.webPageLogo) this.webPageLogo = sysconfig.webPageLogo;
      if (sysconfig.favicon) this.favicon = sysconfig.favicon;

      window.localStorage.setItem("sysconfig", JSON.stringify(sysconfig));
    },
    clearSysConfig() {
      this.$reset();
    }
  }
});
